# -*- coding: utf-8 -*-

"""
Created on 2021/6/4 10:59

@File: export_stationname_account_pwd.py
@Author: LT
@Description: 这个模块很简单，就是根据已有的信息，导出成一个设定好格式的txt文档
              站名 账号 密码 objectId siteId   其中用\t制表符来分隔
"""
import os
import time


class Exporter:
    def __init__(self, iterable, **kwargs):
        ######################################
        # 准备工作
        ######################################
        export_file_name = time.strftime('%Y-%m-%d %H时%M分%S秒')  # 导出的日期名字
        export_file_name += '_export.txt'
        dir_name = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        export_file_dir = os.path.join(dir_name, 'about_logging', 'Export')
        if not os.path.exists(export_file_dir):
            os.mkdir(export_file_dir)
        export_file_abs_name = os.path.join(export_file_dir, export_file_name)

        self.path = export_file_abs_name
        self.iterable = (each for each in iterable)         # 用生成器来做

    def process(self):
        with open(self.path, mode='w', encoding='utf-8') as fbj:
            for each_dict in self.iterable:
                station_name = str(each_dict.get('station_name'))
                renter_account = str(each_dict.get('renter_account'))
                renter_pwd = str(each_dict.get('renter_password'))
                objectId = str(each_dict.get('objectId'))
                siteId = str(each_dict.get('siteId'))
                content_line = station_name + '\t' + renter_account + '\t' + renter_pwd + '\t' + objectId + '\t' + siteId + '\n'
                fbj.write(content_line)


if __name__ == '__main__':
    exp = Exporter([
        {'objectId': '26de8b3944806000',
         'siteId': 'site_00009889',
         'station_name': '温州佳能五金装饰有限公司',
         'userId1': 'ZJ_016587',
         'userId2': 'ZJ_016588',
         'renter_account': "123",
         'renter_pwd': "321"
         }
    ])
    exp.process()
